package com.cwh.controller;

import java.util.Arrays;
import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cwh.entity.Employee;
import com.cwh.entity.EmployeeCustom;
import com.cwh.entity.MenuTree;
import com.cwh.entity.SysPermission;
import com.cwh.entity.SysRole;
import com.cwh.mapper.SysPermissionMapper;
import com.cwh.mapper.SysRoleMapper;
import com.cwh.service.EmployeeService;
import com.cwh.service.SysService;
import com.cwh.utils.MD5Utils;
import com.cwh.utils.UUIDUtils;


@Controller
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private SysService sysService;
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	@Autowired
	private SysPermissionMapper sysPermissionMapper;
	
	//简单查看用户列表
	@RequestMapping("/findUserList")
	public ModelAndView findUserList() {
		ModelAndView mv=new ModelAndView();
		//用户列表自连接查上级名字联表查角色
		List<EmployeeCustom> userAndRoleList = employeeService.findUserAndRoleList();
		mv.addObject("userList", userAndRoleList);
		//角色列表
		List<SysRole> roleList = sysService.findAllSysRole();
		mv.addObject("allRoles",roleList);
		mv.setViewName("userlist");
		return mv;
	}
	
	//添加用户
	@RequestMapping("/saveUser")
	public ModelAndView saveUser(Employee employee) {
		employee.setSalt(MD5Utils.MD5_SALT);
		System.out.println(employee);
		//添加用户信息,给予默认角色与权力
		employeeService.addEmployeeAssignRoleAndPermission(employee);
		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/findUserList");
		return mv;
	}
	
	//跳转到添加角色界面
	@RequestMapping("toAddRole")
	public ModelAndView toAddRole() {
		//父目录嵌套子目录的权限
		List<MenuTree> allPermissions = sysService.loadMenuTree();
		//父目录的
		List<SysPermission> menus = sysService.findAllMenus();
		//根据权限表与角色表联查
		List<SysRole> permissionList = sysService.findRolesAndPermissions();
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("allPermissions", allPermissions);
		mv.addObject("menuTypes", menus);
		mv.addObject("roleAndPermissionsList", permissionList);
		mv.setViewName("rolelist");
		return mv;
	}
	
	/**
	 * 添加角色并给角色赋予权限
	 * @param sysRole 接受角色名字
	 * @param permissionIds 权限复选框
	 * @return
	 */
	@RequestMapping("/saveRoleAndPermissions")
	public String saveRoleAndPermissions(SysRole sysRole,int[] permissionIds) {
		//添加角色
		sysRole.setId(UUIDUtils.getRandomNumber());
		sysRole.setAvailable("1");
		System.out.println("数据："+sysRole);
		System.out.println(Arrays.toString(permissionIds));
		//给角色添加所选择的权限
		sysService.addRoleAndPermissions(sysRole, permissionIds);
		//重定向路径findRoles
		return "redirect:/toAddRole";
	}
	
	//添加新的权限
	@RequestMapping("/saveSubmitPermission")
	public String saveSubmitPermission(SysPermission permission) {
		//设置Available默认为0
		if (permission.getAvailable() == null) {
			permission.setAvailable("0");
		}
		//添加权限
		sysService.addSysPermission(permission);
		//重定向路径findRoles
		return "redirect:/toAddRole";
	}
	
	//用户角色列表
	@RequestMapping("/findRoles")
	public ModelAndView findRoles() {
		ModelAndView mv=new ModelAndView();
		List<SysRole> allSysRole = sysService.findAllSysRole();
		List<MenuTree> allMenuAndPermision = sysService.getAllMenuAndPermision();
		mv.addObject("allRoles", allSysRole);
		mv.addObject("allMenuAndPermissions", allMenuAndPermision);
		mv.setViewName("permissionlist");
		return mv;
	}
	
	//删除角色
	@RequestMapping("/deleteRoleById/{id}")
	public String deleteRoleById(@PathVariable ("id") String id) {
		//根据id删除角色
		sysRoleMapper.deleteByPrimaryKey(id);
		//重定向路径findRoles
		return "redirect:/findRoles";
	}

	//根据角色号显示该角色所具有的权限
	@RequestMapping("/loadMyPermissions")
	@ResponseBody
	public List<SysPermission> loadMyPermissions(String roleId) {
		System.out.println("roleId:===>"+roleId);
		List<SysPermission> findPermissionsByRoleId = sysService.findPermissionsByRoleId(roleId);
		return findPermissionsByRoleId;
	}
	
	//修改角色对应的权限
	@RequestMapping("/updateRoleAndPermission")
	public ModelAndView updateRoleAndPermission(String roleId,String []permissionIds) {
		ModelAndView mv=new ModelAndView();
		sysService.updateRoleAndPermission(roleId,permissionIds);
		mv.setViewName("redirect:/findRoles");
		return mv;
	}
	
	
	
	
	
	
}
